package org.morningangel.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import javax.naming.NamingException;

import org.morningangel.bean.ActivityImg;
import org.morningangel.dao.ActivityImgDao;
import org.morningangel.util.DBHelperUtil;

public class ActivityImgDaoImpl implements ActivityImgDao {

	@Override
	public Integer save(ActivityImg activityImg) {
		Connection conn = null;
		PreparedStatement ps = null;
		Integer suNum = 0;
		try {
			conn = DBHelperUtil.getConnection();
			// 打开事务
			conn.setAutoCommit(false);
			String sql = "insert into tb_activityImg (small, big, activityId) values (?, ?, ?)";
			ps = conn.prepareStatement(sql);
			ps.setString(1, activityImg.getSmall());
			ps.setString(2, activityImg.getBig());
			ps.setInt(3, activityImg.getActivityId());
			suNum = ps.executeUpdate();
			// 提交事务
			conn.commit();
			// 关闭事务
			conn.setAutoCommit(true);

		} catch (NamingException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		}

		DBHelperUtil.close(ps, conn);

		return suNum;
	}

	@Override
	public ActivityImg getById(Integer id) {
		Connection conn = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		ActivityImg activityImg = null;
		try {
			conn = DBHelperUtil.getConnection();
			String sql = "select id, small, big, activityId from tb_activityImg where id = ?";
			ps = conn.prepareStatement(sql);
			ps.setInt(1, id);
			rs = ps.executeQuery();
			if (rs.next()) {
				activityImg = new ActivityImg();
				setActivityImg(rs, activityImg);
			}
		} catch (NamingException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
		DBHelperUtil.close(rs, ps, conn);
		
		return activityImg;
	}

	@Override
	public Integer update(ActivityImg activityImg) {
		Connection conn = null;
		PreparedStatement ps = null;
		Integer suNum = 0;
		try {
			conn = DBHelperUtil.getConnection();
			// 打开事务
			conn.setAutoCommit(false);
			String sql = "update tb_activityImg set small = ?, big = ? where id = ?";
			ps = conn.prepareStatement(sql);
			ps.setString(1, activityImg.getSmall());
			ps.setString(2, activityImg.getBig());
			ps.setInt(3, activityImg.getId());
			suNum = ps.executeUpdate();
			// 提交事务
			conn.commit();
			// 关闭事务
			conn.setAutoCommit(true);
		} catch (NamingException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
		DBHelperUtil.close(ps, conn);
		
		return suNum;
	}

	@Override
	public List<ActivityImg> getActivityImgs(Integer activityId) {
		Connection conn = null;
		PreparedStatement ps = null;
		List<ActivityImg> list = new ArrayList<ActivityImg>();
		ResultSet rs = null;
		try {
			conn = DBHelperUtil.getConnection();
			String sql = "select id, small, big, activityId from tb_activityImg where activityId = ?";
			ps = conn.prepareStatement(sql);
			ps.setInt(1, activityId);
			rs = ps.executeQuery();
			while(rs.next()) {
				ActivityImg activityImg = new ActivityImg();
				setActivityImg(rs, activityImg);
				list.add(activityImg);
			}
		} catch (NamingException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
		DBHelperUtil.close(rs, ps, conn);
		
		return list;
	}
	
	private void setActivityImg(ResultSet rs, ActivityImg activityImg) throws SQLException {
		activityImg.setId(rs.getInt(1));
		activityImg.setSmall(rs.getString(2));
		activityImg.setBig(rs.getString(3));
		activityImg.setActivityId(rs.getInt(4));
	}

}
